package com.zxc.item.controller;

import com.zxc.common.enums.ShoppingEnum;
import com.zxc.common.exception.ShoppingException;
import com.zxc.item.entity.Item;
import com.zxc.item.service.impl.ItemServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ItemController {

    @Autowired
    private ItemServiceImpl itemServiceImpl;

    @PostMapping("/add")
    public ResponseEntity<Item> saveItem(Item item){

        //如果item中的price属性为空
        if(item.getPrice() == null){
            //不能添加
            //return ResponseEntity.status(HttpStatus.BAD_REQUEST.value()).body(null);
            //return ResponseEntity.status(HttpStatus.BAD_REQUEST.value()).build();
            throw new ShoppingException(ShoppingEnum.PRICE_NOT_BE_NULL);
        }

        item = itemServiceImpl.saveItem(item);
        return ResponseEntity.status(HttpStatus.CREATED.value()).body(item);
    }
}
